const logger = require("../utils/logger")

module.exports = async (ctx, next) => {
    try {
        await next();
    } catch (err) {
      // Error 对象
      if (err.message instanceof Error) {
        err.message = err.message.toString()
      }
      // 普通json对象
      if (typeof(err.message) == "object") {
        err.message = JSON.stringify(err.message)
      }
      logger.error([
          `traceId: ${err.traceId}`,
          `status: ${err.status}`,
          `message: ${err.message}`
      ].join(' | '))
      ctx.$fail(err)
    }
};
